<template>
  <div class="app-container">
    <el-form ref="formRef" :model="form" label-width="100px">
      <el-row :gutter="20">
        <el-col :span="6">
          <el-form-item label="工艺段编号" prop="sectionCode">
            {{ form.sectionCode }}
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="工艺段名称" prop="sectionName">
            {{ form.sectionName }}
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="工厂" prop="factoryName">
            {{ form.factoryName }}
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="工段" prop="workshopName">
            {{ form.workshopName }}
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="产线" prop="productLineName">
            {{ form.productLineName }}
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="备注" prop="productLineName">
            <el-tooltip
              popper-class="my-tooltip"
              class="box-item"
              effect="dark"
              :content="form.remark"
              placement="top-start"
            >
              <p style="margin: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap">
                {{ form.remark }}
              </p>
            </el-tooltip>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <el-tabs v-hasPermi="['baseinfo:station:list']" v-model="activeName" class="demo-tabs">
      <el-tab-pane label="所属岗位" name="first">
        <el-form
          :model="queryParams"
          ref="queryRef"
          :inline="true"
          v-show="showSearch"
          label-width="100px"
        >
          <el-form-item label="岗位名称" prop="stationName">
            <el-input
              v-model="queryParams.stationName"
              placeholder="请输入岗位名称"
              clearable
              style="width: 240px"
              @keyup.enter="handleQuery"
            />
          </el-form-item>

          <el-form-item>
            <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
            <el-button icon="Refresh" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button
              type="primary"
              plain
              icon="Plus"
              @click="handleAdd"
              v-hasPermi="['baseinfo:station:add']"
            >
              添加
            </el-button>
          </el-col>

          <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>
        <el-table :data="tableData" border>
          <el-table-column
            label="岗位编号"
            align="center"
            prop="stationCode"
            :show-overflow-tooltip="true"
          ></el-table-column>
          <el-table-column
            label="岗位名称"
            align="center"
            prop="stationName"
            :show-overflow-tooltip="true"
          />

          <el-table-column
            label="岗位描述"
            align="center"
            prop="remark"
            :show-overflow-tooltip="true"
          />
          <el-table-column
            label="操作"
            align="center"
            width="160"
            class-name="small-padding fixed-width"
          >
            <template #default="scope">
              <el-button
                link
                type="primary"
                icon="Delete"
                @click="handleDelete(scope.row, scope.$index)"
                v-hasPermi="['baseinfo:station:remove']"
              >
                移除
              </el-button>
            </template>
          </el-table-column>
        </el-table>

        <pagination
          v-show="total > 0"
          :total="total"
          v-model:page="queryParams.pageNum"
          v-model:limit="queryParams.pageSize"
          @pagination="getList"
        />
      </el-tab-pane>
    </el-tabs>
    <stationDialog v-model:show="showDialog" @change="handleChange"></stationDialog>
  </div>
</template>

<script setup name="Data">
import stationDialog from "@/components/DialogSelect/stationDialog.vue";
import { getProdSection, getStation, addStation, delStation } from "@/api/base/processSection";
import { checkPermi } from "@/utils/permission";
import { ref, reactive, useAttrs, toRefs, getCurrentInstance, onMounted } from "vue";
const { proxy } = getCurrentInstance();
const showSearch = ref(true);
const showDialog = ref(false);
const attrs = useAttrs();
const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    sectionCode: undefined,
  },
});
const tableData = ref([]);
const activeName = ref("first");
const total = ref(0);
const formRef = ref(null);
const { form, queryParams } = toRefs(data);

function getDetail(id) {
  reset();
  getProdSection(id).then((res) => {
    form.value = res.data;
    queryParams.value.sectionCode = form.value.sectionCode;
    if (checkPermi(["baseinfo:station:list"])) {
      getList();
    }
  });
}

function handleAdd() {
  showDialog.value = true;
}

function handleChange(val) {
  if (val.length) {
    const stationCodes = tableData.value.map((item) => item.stationCode);
    const dataList = [];
    val.forEach((el) => {
      if (!stationCodes.includes(el.workStationCode)) {
        dataList.push({
          sectionCode: form.value.sectionCode,
          sectionName: form.value.sectionName,
          stationCode: el.workStationCode,
          stationName: el.workStationName,
          remark: el.workStationDesc,
        });
      }
    });
    if (!dataList.length) {
      proxy.$modal.msgError("岗位重复添加");
      return;
    }
    addStation(dataList).then(() => {
      proxy.$modal.msgSuccess("添加成功");
      getList();
    });
  }
}

function getList() {
  getStation(queryParams.value).then((response) => {
    tableData.value = response.rows;
    total.value = response.total;
  });
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

/** 移除按钮操作 */
function handleDelete(row, index) {
  proxy.$modal
    .confirm('是否确认移除岗位编号为"' + row.stationCode + '"的数据项？')
    .then(() => {
      if (row.id) {
        delStation(row.id).then(() => {
          tableData.value = tableData.value.filter((item) => {
            return item.id !== row.id;
          });
          proxy.$modal.msgSuccess("移除成功");
          getList();
        });
      } else {
        tableData.value.splice(index, 1);
        proxy.$modal.msgSuccess("移除成功");
        getList();
      }
    })

    .catch(() => {});
}

/** 表单重置 */
function reset() {
  form.value = {
    name: undefined,
    type: undefined,
    equipNo: undefined,
    model: undefined,
    factory: undefined,
    segmentCode: undefined,
    workshopCode: undefined,
    status: undefined,
    remark: undefined,
  };
  formRef.value.resetFields();
}
onMounted(async () => {
  const { data } = attrs;
  getDetail(data.id);
});
</script>
<style>
.my-tooltip {
  max-width: 400px !important;
}
</style>
